﻿using AutoMapper.Internal.Mappers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Himp.Platform.Dc
{
    /// <summary>
    /// 收费类别分组服务
    /// </summary>
    public class ChrgitmTypeGrpService : CrudAppService<ChrgitmTypeGrp, ChrgitmTypeGrpDto, Guid, ChrgitmTypeGrpInput, ChrgitmTypeGrpCreateDto, ChrgitmTypeGrpUpdateDto>
    {
        private IRepository<ChrgitmTypeGrp, Guid> _chrgitmTypeGrpRepository;

        public ChrgitmTypeGrpService(IRepository<ChrgitmTypeGrp, Guid> chrgitmTypeGrpRepository) : base(chrgitmTypeGrpRepository)
        {
            _chrgitmTypeGrpRepository = chrgitmTypeGrpRepository;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public override async Task<PagedResultDto<ChrgitmTypeGrpDto>> GetListAsync(ChrgitmTypeGrpInput input)
        {
            var iQuery = await _chrgitmTypeGrpRepository.GetQueryableAsync();
            //iQuery = iQuery.WhereIf(!string.IsNullOrEmpty(input.Key), d => d.ChrgitmTypeGrpCodg.Contains(input.Key) || d.ChrgitmTypeGrpName.Contains(input.Key));

            if (input.SkipCount == 0)
            {
                var itemsDto = ObjectMapper.Map<List<ChrgitmTypeGrp>, List<ChrgitmTypeGrpDto>>(iQuery.ToList());

                return new PagedResultDto<ChrgitmTypeGrpDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.SkipCount, input.MaxResultCount);
                var itemsDto = ObjectMapper.Map<List<ChrgitmTypeGrp>, List<ChrgitmTypeGrpDto>>(items.ToList());

                return new PagedResultDto<ChrgitmTypeGrpDto>(totalCount, itemsDto);
            }
        }
    }
}
